//
//  FPVeriViewController.m
//  Gunz
//
//  Created by Joggy on 16/2/1.
//  Copyright © 2016年 NUPT. All rights reserved.
//

#import "FPVeriViewController.h"

static NSString *accountName = @"";
static CGFloat textBottom = 0;
static CGRect viewOrigin;

@implementation FPVeriViewController

-(void)viewDidLoad {
    [super viewDidLoad];
    [self prepareForNav];
    [self prepareForLayout];
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] init];
    [tap addTarget:self action:@selector(endEdit)];
    [self.view addGestureRecognizer:tap];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
}

-(void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    viewOrigin = self.view.frame;
}

-(void)prepareForNav {
    [super prepareForNav];
    self.title = @"找回密码";
    //返回按键
    UIImage *leftImage = [UIImage imageNamed:@"all_back"];
    UIButton *leftButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, (leftImage.size.width/leftImage.size.height)*22, 22)];
    [leftButton setImage:leftImage forState:UIControlStateNormal];
    [leftButton addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *leftBarItem = [[UIBarButtonItem alloc] initWithCustomView:leftButton];
    [self navigationItem].leftBarButtonItem = leftBarItem;
}

-(void)prepareForLayout {
    [super prepareForLayout];
    veriState = [[GZOperationState alloc] initWithFrame:CGRectMake(0, 0, OSWidth, OSHeight) state:1];
    [self.view addSubview:veriState];
    scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, OSHeight, screenWidth, screenHeight - BBViewHeight - OSHeight)];
    scroll.contentSize = CGSizeMake(screenWidth, scroll.frame.size.height + 1);
    scroll.delegate = self;
    UIView *accountView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 66, FPVTextHeihgt)];
    //label
    accountLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 66, 66)];
    NSMutableAttributedString *accountTitle = [[NSMutableAttributedString alloc] initWithString:@"账尼玛号 : "];
    NSRange titleRange1 = {0, accountTitle.length};
    NSRange titleRange2 = {1, 2};
    [accountTitle addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:font15*proportion] range:titleRange1];
    [accountTitle addAttribute:NSForegroundColorAttributeName value:[UIColor clearColor] range:titleRange2];
    accountLabel.attributedText = accountTitle;
    accountLabel.alpha = 0.7;
    [accountLabel sizeToFit];
    accountLabel.layer.position = CGPointMake(accountLabel.frame.size.width/2, accountView.frame.size.height/2);
    [accountView addSubview:accountLabel];
    //账号
    account = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 66, 66)];
    accountTitle = [[NSMutableAttributedString alloc] initWithString:accountName];
    NSRange titleRange3 = {0, accountTitle.length};
    [accountTitle addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"Helvetica-Bold" size:font15*proportion] range:titleRange3];
    account.attributedText = accountTitle;
    [account sizeToFit];
    account.layer.position = CGPointMake(accountLabel.frame.size.width + account.frame.size.width/2, accountLabel.layer.position.y);
    [accountView addSubview:account];
    //调整accountView
    accountView.frame = CGRectMake(0, 0, accountLabel.frame.size.width + account.frame.size.width, FPVTextHeihgt);
    accountView.layer.position = CGPointMake(screenWidth/2, accountView.frame.size.height/2 + FPVInsert);
    [scroll addSubview:accountView];
    //密码视图
    UIView *pwView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 66, FPVTextHeihgt)];
    //密码标题
    newPWLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 66, 66)];
    //密码的属性字符
    NSMutableAttributedString *pwTitle = [[NSMutableAttributedString alloc] initWithString:@"新  密  码 : "];
    NSRange ran1 = {0, pwTitle.length};
    [pwTitle addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:font15*proportion] range:ran1];
    newPWLabel.attributedText = pwTitle;
    newPWLabel.alpha = 0.7;
    [newPWLabel sizeToFit];
    newPWLabel.layer.position = CGPointMake(newPWLabel.frame.size.width/2, pwView.frame.size.height/2);
    [pwView addSubview:newPWLabel];
    //密码输入框
    newPWText = [[UITextField alloc] initWithFrame:CGRectMake(newPWLabel.frame.size.width, 0, FPVTextWidth, FPVTextHeihgt)];
    newPWText.secureTextEntry = true;
    newPWText.leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, FPVInsert/4, FPVTextHeihgt)];
    newPWText.font = [UIFont systemFontOfSize:font14*proportion];
    newPWText.leftViewMode = UITextFieldViewModeAlways;
    newPWText.backgroundColor = [UIColor whiteColor];
    newPWText.delegate = self;
    newPWText.layer.borderColor = [UIColor colorWithRed:0.83 green:0.83 blue:0.83 alpha:1].CGColor;
    newPWText.layer.borderWidth = 1;
    newPWText.layer.cornerRadius = 6;
    [pwView addSubview:newPWText];
    pwView.frame = CGRectMake(0, 0, newPWLabel.frame.size.width + newPWText.frame.size.width, FPVTextHeihgt);
    pwView.layer.position = CGPointMake(screenWidth/2, accountView.frame.origin.y + accountView.frame.size.height + pwView.frame.size.height/2 + FPVInsert/4);
    [scroll addSubview:pwView];
    
    //重复密码框
    dupView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 66, FPVTextHeihgt)];
    //密码标题
    dupPWLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 66, 66)];
    dupPWLabel.text = @"重复密码 : ";
    dupPWLabel.font = [UIFont systemFontOfSize:font15*proportion];
    dupPWLabel.alpha = 0.7;
    [dupPWLabel sizeToFit];
    dupPWLabel.layer.position = CGPointMake(dupPWLabel.frame.size.width/2, dupView.frame.size.height/2);
    [dupView addSubview:dupPWLabel];
    //密码输入框
    dupPWText = [[UITextField alloc] initWithFrame:CGRectMake(dupPWLabel.frame.size.width, 0, FPVTextWidth, FPVTextHeihgt)];
    dupPWText.secureTextEntry = true;
    dupPWText.leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, FPVInsert/4, FPVTextHeihgt)];
    dupPWText.font = [UIFont systemFontOfSize:font14*proportion];
    dupPWText.leftViewMode = UITextFieldViewModeAlways;
    dupPWText.backgroundColor = [UIColor whiteColor];
    dupPWText.tag = 1;
    dupPWText.delegate = self;
    dupPWText.layer.borderColor = [UIColor colorWithRed:0.83 green:0.83 blue:0.83 alpha:1].CGColor;
    dupPWText.layer.borderWidth = 1;
    dupPWText.layer.cornerRadius = 6;
    [dupView addSubview:dupPWText];
    dupView.frame = CGRectMake(0, 0, dupPWLabel.frame.size.width + dupPWText.frame.size.width, FPVTextHeihgt);
    dupView.layer.position = CGPointMake(screenWidth/2, pwView.frame.origin.y + pwView.frame.size.height + dupView.frame.size.height/2 + FPVInsert/4);
    [scroll addSubview:dupView];
    //调整accountView和pwView的位置
    CGRect temp = accountView.frame;
    temp.origin.x = dupView.frame.origin.x;
    accountView.frame = temp;
    temp = pwView.frame;
    temp.origin.x = dupView.frame.origin.x + (dupView.frame.size.width - pwView.frame.size.width);
    pwView.frame = temp;
    [self.view addSubview:scroll];
    //底部按键
    changeBarButton = [[BarButtonView alloc] initWithFrame:CGRectMake(0, screenHeight - 64 - BBViewHeight, screenWidth, BBViewHeight) name:@"修改密码"];
    [changeBarButton.but addTarget:self action:@selector(changePW) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:changeBarButton];
}

-(void)back {
    [self.navigationController popViewControllerAnimated:true];
}

-(void)passAccount:(NSString *)passAccount {
    accountName = passAccount;
}

-(void)changePW {
    [self.navigationController dismissViewControllerAnimated:true completion:nil];
}

-(void)endEdit {
    [self.view endEditing:true];
}

//键盘监听事件

-(void)keyboardWillShow:(NSNotification*)notification {
    NSDictionary *notifiInfo = notification.userInfo;
    CGFloat keyboardHeight = [[notifiInfo objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size.height;
    CGRect viewFrame = viewOrigin;
    CGFloat adjust = textBottom - (screenHeight - keyboardHeight - 64);
    if (adjust > 0) {
        viewFrame.origin.y -= adjust;
        [UIView animateWithDuration:0.3 animations:^{
            self.view.frame = viewFrame;
        }];
    }
    else {
        [UIView animateWithDuration:0.3 animations:^{
            self.view.frame = viewOrigin;
        }];
    }
}

-(void)keyboardWillHide:(NSNotification*)notification {
    [UIView animateWithDuration:0.3 animations:^{
        self.view.frame = viewOrigin;
    }];
}

-(void)scrollViewWillBeginDragging:(UIScrollView*)scrollView {
    [self endEdit];
}

-(void)textFieldDidBeginEditing:(UITextField *)textField {
    if (textField.tag == 1) {
        textBottom = scroll.frame.origin.y + dupView.frame.origin.y + textField.frame.origin.y + textField.frame.size.height;
    }
    else {
        textBottom = 0;
    }
}

@end
